package com.oplink.webems.modules.log.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.oplink.webems.common.utils.DateUtils;
import com.oplink.webems.common.utils.UserUtils;
import com.oplink.webems.modules.log.entity.UserLog;
import com.oplink.webems.modules.log.service.UserLogService;
import com.oplink.webems.modules.sys.entity.User;

@Aspect
@Component
public class UserLogAop extends BaseLogAop {
	
	@Autowired
	UserLogService us ;
	
	@Autowired
	public void setUs(UserLogService us) {
		this.us = us;
	}

	@Pointcut("execution(* com.oplink.webems.modules.*.service.*.*(..))")
	public void serviceAop(){}
	
	
	@Before("serviceAop()")
	public void beforeAdvice1(JoinPoint joinPoint){
		//System.out.println("aop1 ---------------------- "+joinPoint.getThis()+";"+joinPoint.getSourceLocation()+";"+joinPoint.getTarget()+";"+joinPoint.getArgs()+";"+joinPoint.getKind()+";"+joinPoint.getSignature());
		Object o = joinPoint.getTarget();
		Signature s = joinPoint.getSignature();
		Object[] args = joinPoint.getArgs();
		
		UserLog ul = new UserLog();
		int module = getModule(o);
		if(module == AopModulesConstant.NOAOPED || module == 0){
			return;
		}
		
		ul.setModule(module);
		try{
			//如果获取不到用户，说明该操作不是用户发起的。可能是系统自发的，如Trap， 应该不记录到用户日志，可记录到通讯 日志。
			UserUtils.getCurrentUserId().intValue();
		}catch(Exception e){
			return ;
		}
		ul.setOwnerId(UserUtils.getCurrentUserId().intValue());
		ul.setUser(new User(UserUtils.getCurrentUserId()));
		ul.setParams(getParams(args));
		ul.setDesci(getDesc(s));
		ul.setTimei(DateUtils.getCalendar(DateUtils.getDateTime()));
		
		us.saveUserLog(ul);
	}
	
}
